<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/31
 * Time: 11:04
 */

namespace App\Handler;

use App\Models\Info;
use Illuminate\Support\Facades\DB;
use p5\objects\ArrayUtils;
use p5\objects\StringUtils;


class InfoHandler
{

    static public function saveContent($organizationId, $type_id, $content, $deleteFirst = false)
    {
        $result = 1;
        if ($deleteFirst) {
            Info::where("organization_id", $organizationId)->where("type_id", $type_id)->delete();
        }
        $matrix = StringUtils::explodeByLen($content, 5000);
        $index = 1;
        foreach ($matrix as $string) {
            $bean = new Info();
            $bean->set_organization_id($organizationId);
            $bean->set_type_id($type_id);
            $bean->set_content($string);
            $bean->set_sequence($index);
            $bean->save();
            $index++;
        }
        return $result;
    }


    static public function getContent($organizationId, $type_id)
    {
        $list = Info::where("organization_id", $organizationId)->where("type_id", $type_id)->get();
        if (empty($list)) {
            return "";
        }
        $content = ArrayUtils::pick($list, "content");
        return implode("", $content);
    }


}
